*** Driving While Unauthorized: Auto Insurance Remains Unchanged When Providing Driver Licenses to Unauthorized Immigrants in California
*** Journal of Risk and Insurance
*** Hans Lueders and Micah Mumper
*** Power Analysis
*** 10 April 2022


*--------------------*
*** 1. Preparation ***
*--------------------*

*** load data
use "LuedersMumperJRI_DataForPoweranalysis", clear

*** encode counties
encode county, gen(county2)

*** create interctions between exposure median and treatment pre/post
gen exposure_median1 = 0
replace exposure_median1 = 1 if exposure_median == 1 & treat == 1

*** save
save dataBoot.dta, replace






*---------------------------*
*** 2. Uninsured Vehicles ***
*---------------------------*

*** a. Share uninsured vehicles ***
*---------------------------------*

* what's the treatment effect in the data?
use "dataBOOT.dta", clear
quietly: reghdfe uninsured i.exposure_median treat exposure_median1, a(county2 year) vce(cluster county2)

local placebo = _b[exposure_median1]


* Set up the output container
tempname results
postfile `results' effect_size b se using "power_uptake_uninsuredAUX.dta", replace

* Loop over the effect size and bootstrap draws
forval te=0(0.001)0.031{
    use "dataBOOT.dta", clear
	
	dis `te' 
	
	gen dvBoot = uninsured
	replace dvBoot = dvBoot + `te' if exposure_median == 1 & treat == 1	

	forval i=1/2500 {
		preserve
		bsample, strata(exposure_median) cluster(county2)
		quietly: reghdfe dvBoot i.exposure_median treat exposure_median1, a(county2 year) vce(cluster county2)
		post `results' (`te') (_b[exposure_median1] - `placebo') (_se[exposure_median1])
		dis `i'
		restore
	}

}

* Close the results log
postclose `results'

* Calculate the power by effect size
use "power_uptake_uninsuredAUX.dta", clear
gen significant = !inrange(b/se, -1.96, 1.96)
replace significant = 0 if b < 0 & effect_size > 0
collapse significant b, by(effect_size)
saveold "power_uptake_uninsured.dta", replace version(12)



*** b. Claims for Uninsured Motorist BI, per 1,000 vehicles ***
*-------------------------------------------------------------*

* what's the treatment effect in the data?
use "dataBOOT.dta", clear
quietly: reghdfe ClaimsNormStandUMBIBasic_log i.exposure_median treat exposure_median1, a(county2 year) vce(cluster county2)

local placebo = _b[exposure_median1]

* Set up the output container
tempname results
postfile `results' effect_size b se using "power_uptake_umbiAUX.dta", replace

* Loop over the effect size and bootstrap draws
forval te=0(0.01)0.6{
    use "dataBOOT.dta", clear
	
	dis `te' 
	
	gen dvBoot = ClaimsNormStandUMBIBasic_log
	replace dvBoot = dvBoot + `te' if exposure_median == 1 & treat == 1	

	forval i=1/2500 {
		preserve
		bsample, strata(exposure_tercile) cluster(county2)
		quietly: reghdfe dvBoot i.exposure_median treat exposure_median1, a(county2 year) vce(cluster county2)
		post `results' (`te') (_b[exposure_median1] - `placebo') (_se[exposure_median1])
		dis `i'
		restore
	}

}

* Close the results log
postclose `results'

* Calculate the power by effect size
use "power_uptake_umbiAUX.dta", clear
gen significant = !inrange(b/se, -1.96, 1.96)
replace significant = 0 if b < 0 & effect_size > 0
collapse significant b, by(effect_size)
saveold "power_uptake_umbi.dta", replace version(12)






*---------------------------*
*** 3. Insurance Premiums ***
*---------------------------*

*** a. Premiums for BI ***
*------------------------*

* what's the treatment effect in the data?
use "dataBOOT.dta", clear
quietly: reghdfe AvgPremiumStandBIBasic_log i.exposure_median treat exposure_median1, a(county2 year) vce(cluster county2)

local placebo = _b[exposure_median1]

* Set up the output container
tempname results
postfile `results' effect_size b se using "power_premiums_biAUX.dta", replace

* Loop over the effect size and bootstrap draws
forval te=0(0.005)0.105 {
    use "dataBOOT.dta", clear
	
	dis `te' 
	
	gen premiums_Boot = AvgPremiumStandBIBasic_log
	replace premiums_Boot = premiums_Boot + `te' if exposure_median == 1 & treat == 1

	forval i=1/2500 {
		preserve
		bsample, strata(exposure_median) cluster(county2)
		quietly: reghdfe premiums_Boot i.exposure_median treat exposure_median1, a(county2 year) vce(cluster county2)
		post `results' (`te') (_b[exposure_median1] - `placebo') (_se[exposure_median1])
		dis `i'
		restore
	}

}

* Close the results log
postclose `results'


* Calculate the power by effect size
use "power_premiums_biAUX.dta", clear
gen significant = !inrange(b/se, -1.96, 1.96)
replace significant = 0 if b < 0 & effect_size > 0
collapse significant b, by(effect_size)
saveold "power_premiums_bi.dta", replace version(12)




*** b. Premiums for Inexperienced drivers ***
*-------------------------------------------*

* what's the treatment effect in the data?
use "dataBOOT.dta", clear
quietly: reghdfe premium1both_log i.exposure_median treat exposure_median1, a(county2 year) vce(cluster county2)

local placebo = _b[exposure_median1]


* Set up the output container
tempname results
postfile `results' effect_size b se using "power_premiums_inexperiencedAUX.dta", replace

* Loop over the effect size and bootstrap draws
forval te=0(0.001)0.0301{
    use "dataBOOT.dta", clear
	
	dis `te' 
	
	gen premiums_Boot = premium1both_log
	replace premiums_Boot = premiums_Boot + `te' if exposure_median == 1 & treat == 1

	forval i=1/2500 {
		preserve
		bsample, strata(exposure_median) cluster(county2)
		quietly: reghdfe premiums_Boot i.exposure_median treat exposure_median1, a(county2 year) vce(cluster county2)
		post `results' (`te') (_b[exposure_median1] - `placebo') (_se[exposure_median1])
		dis `i'
		restore
	}

}

* Close the results log
postclose `results'

* Calculate the power by effect size
use "power_premiums_inexperiencedAUX.dta", clear
gen significant = !inrange(b/se, -1.96, 1.96)
replace significant = 0 if b < 0 & effect_size > 0
collapse significant b, by(effect_size)
saveold "power_premiums_inexperienced.dta", replace version(12)




*** c. Premiums for Experienced drivers ***
*-----------------------------------------*

* what's the treatment effect in the data?
use "dataBOOT.dta", clear
quietly: reghdfe premium7both_log i.exposure_median treat exposure_median1, a(county2 year) vce(cluster county2)

local placebo = _b[exposure_median1]


* Set up the output container
tempname results
postfile `results' effect_size b se using "power_premiums_experiencedAUX.dta", replace

* Loop over the effect size and bootstrap draws
forval te=0(0.001)0.0301{
    use "dataBOOT.dta", clear
	
	dis `te' 
	
	gen premiums_Boot = premium7both_log
	replace premiums_Boot = premiums_Boot + `te' if exposure_median == 1 & treat == 1

	forval i=1/2500 {
		preserve
		bsample, strata(exposure_median) cluster(county2)
		quietly: reghdfe premiums_Boot i.exposure_median treat exposure_median1, a(county2 year) vce(cluster county2)
		post `results' (`te') (_b[exposure_median1] - `placebo') (_se[exposure_median1])
		dis `i'
		restore
	}

}

* Close the results log
postclose `results'

* Calculate the power by effect size
use "power_premiums_experiencedAUX.dta", clear
gen significant = !inrange(b/se, -1.96, 1.96)
replace significant = 0 if b < 0 & effect_size > 0
collapse significant b, by(effect_size)
saveold "power_premiums_experienced.dta", replace version(12)


